﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace Client
{
    public partial class UserManager : BasePopForm
    {
        QunYing.BLL.Employee emlpoyee = new QunYing.BLL.Employee();
        public UserManager()
        {
            InitializeComponent();
            this.listBoxControl1.SelectedIndexChanged += new EventHandler(listBoxControl1_SelectedIndexChanged);
        }

        void listBoxControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBoxControl1.SelectedIndex != -1)
            {
                QunYing.Model.Employee emp = (QunYing.Model.Employee)listBoxControl1.SelectedItem;
                this.txt_密码.Text = emp.密码;
                this.lbl_登录名.Text = emp.用户名;
                this.cke_启用.Checked = emp.是否启用;
            }
        }

        private void UserManager_Load(object sender, EventArgs e)
        {
            this.listBoxControl1.Items.Clear();
            var v = emlpoyee.GetModelList(string.Format(" 所属经销商='{0}'",GlobalInfo.UserLoginInfo.用户ID));
            this.listBoxControl1.Items.AddRange(v.ToArray());
            
        }

        private void btn_Save_Click(object sender, EventArgs e)
        {
            if (listBoxControl1.SelectedIndex != -1)
            {
                if (!string.IsNullOrEmpty(this.txt_密码.Text.Trim()))
                {
                    QunYing.Model.Employee emp = (QunYing.Model.Employee)listBoxControl1.SelectedItem;
                    emp.密码 = this.txt_密码.Text;
                    emp.是否启用 = this.cke_启用.Checked;
                    this.emlpoyee.Update(emp);
                    GlobalInfo.MsgBoxShowInfo("保存已成功！");
                }
                else
                {
                    GlobalInfo.MsgBoxShowError("密码不能为空");
                }
            }
        }

        private void btn_新增_Click(object sender, EventArgs e)
        {
            FormAddUser form = new FormAddUser();
            if (form.ShowDialog() == DialogResult.Yes)
            {
                this.listBoxControl1.Items.Clear();
                var v = emlpoyee.GetModelList(string.Format(" 所属经销商='{0}'", GlobalInfo.UserLoginInfo.用户ID));
                this.listBoxControl1.Items.AddRange(v.ToArray());
            }
        }

        private void txt_重置验证_Click(object sender, EventArgs e)
        {
            if (listBoxControl1.SelectedIndex != -1)
            {
                    QunYing.Model.Employee emp = (QunYing.Model.Employee)listBoxControl1.SelectedItem;
                    emp.验证信息 = null;
                    this.emlpoyee.Update(emp);
                    GlobalInfo.MsgBoxShowInfo("重置已成功！");
                
            }
        }

        private void btn_关闭_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}